<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("Strings.php");					//--> String utils

include_once("customerSession.inc.php");
include_once ("Session.php");					//-- Session

include_once ("Password.php");					//-- Password
include_once ("Validation.php");				//-- Validation

include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

$common 		= new Common();
$objStrings 	= new Strings();
$objSession		= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session

$objValidation	= new Validation();		//--> Validation
$objPassword 	= new Password();
$objCart 		= new Cart();		//--> Cart

session_start();

$isLogin	= false;

if($objSession->exist()) {
	$isLogin	= true;
	
}

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);

/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->debugging 		= false;
$smarty->caching 		= false;



if(isset($_POST['email'])){
	$email 		= $_POST['email'];
	
	if(empty($email)){
		$error_message = 'The email is required.';
	}else if(!$objValidation->isEmail($email)){
		$error_message = 'The email format invalid.';
	}
	
	if(empty($error_message)){
		$customer 	= $common->getRow(DB_TAG_SYSTEM, "SELECT id, firstname, lastname FROM customers WHERE email = '$email'");
	
		if(is_array($customer) && count($customer) > 0){
			$new_password = $objPassword->getRandomValue(6);
			
			$crypted_password = addslashes ($objPassword->encrypt($new_password));
	
	      	$sql = "update customers set password = '$crypted_password' where id = " . (int)$customer['id'];
			$common->update(DB_TAG_SYSTEM, $sql);
			
			$ip = get_client_ip();
			$emailContent = "<div><br>
				A new password was requested from $ip.<br>
				<br>
				Your new password to '<a href='http://123gohelmets.com' target='_blank''>123gohelmets.com</a>' is:<br>
				<br>
				 &nbsp; $new_password<br>
				<br>
				<br>
				<br>
				</div>";
			
			
			$subject	= '123gohelmets.com - New Password';
			
			/* To send HTML mail, you can set the Content-type header. */
			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=utf8\r\n";
			
			/* additional headers */
			$headers .= "From: Sam <atvglove@gmail.com>\r\n";
			
			/* and now mail it */
			$siSuccess = mail($email, $subject, $emailContent, $headers);
			
			if($siSuccess){
		    	$error_message = 'Success: A new password has been sent to your e-mail address.';
			}else
		    	$error_message = 'Failed: get new password failed.';
		}else{
			$error_message = 'Error: The E-Mail Address was not found in our records, please try again.';
		}
	}
}

function get_client_ip()
{
	if ($_SERVER['REMOTE_ADDR']) {
		$cip = $_SERVER['REMOTE_ADDR'];
	} elseif (getenv("REMOTE_ADDR")) {
		$cip = getenv("REMOTE_ADDR");
	} elseif (getenv("HTTP_CLIENT_IP")) {
		$cip = getenv("HTTP_CLIENT_IP");
	} else {
		$cip = "unknown";
	}
	
	return $cip;
}


$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('error_message', $error_message);

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->assign('isLogin', $isLogin);


$smarty->display('passwordForgotten.html');
?>
